package com.scf.common.vo;

import com.scf.common.validator.EnumCheck;
import com.scf.common.validator.RangeCheck;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.Valid;
import javax.validation.constraints.*;
import javax.validation.groups.Default;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author sunchuanfu
 */
@Data
public class DemoInfo implements Serializable {
    private static final long serialVersionUID = 1L;

    // 注：@Size注解验证的是长度，不是大小
    // 注：@Size注解不支持验证数字的最大和最小，只支持下面的几种类型
    // Supported types are:
    // CharSequence (length of character sequence is evaluated)
    // Collection (collection size is evaluated)
    // Map (map size is evaluated)
    // Array (array length is evaluated)
    @Size(min = 1, max = 3, groups = {GroupB.class})

    // @NotBlank(message = "名称不能为空！") // 可以覆盖默认的提示语
    @NotBlank(groups = {GroupA.class}, message = "名称不能为空！")
    // 使用默认的提示语,在hibernate-validate.jar的配置文件中ValidationMessages.properties
    private String name;

    @Max(value = 6, message = "xxx最大长度不能超过{value}")
    @Min(value = 1)
    @NotNull(groups = Default.class)// 默认都是Default分组
    private Integer type;

    @NotBlank(groups = {GroupA.class})// 指定校验所属的组
    private String companyId;

    // https://blog.csdn.net/java_collect/article/details/85534054
    @EnumCheck(message = "性别值只能为1或者2", enumClass = Sex.class)
    private Integer sex;

    @RangeCheck(values = {"1", "2"})
    private String age;

    @DecimalMin(value = "0.01")
    // @Min(value = 0)
    @Digits(integer = 14, fraction = 2)// 包括负数；默认提示：数字的值超出了允许范围(只允许在14位整数和2位小数范围内)
    // @Digits(integer = 14, fraction = 2, message = "${validatedValue}：整数位上限为{integer}位，小数位上限为{fraction}位")// 包括负数
    private String digits;

    // 正数不包括0，不能修饰String
    @Positive
    private Integer positive;

    @PositiveOrZero
    private Long positiveOrZero;

    @Negative
    private Integer negative;

    @DecimalMax(value = "101.1")
    private String decimalMax;

    @Max(value = 100)
    private Integer max;// 最大不能超过100

    // 匹配空串和yyyy-MM-dd
    @Pattern(regexp = "\\s*|((\\d{4})-((1[0-2])|(0[1-9]))-(([12][0-9])|(3[01])|(0[1-9])))", message = "申请日期格式不正确")
    private String applyDateFrom;

    @Future
    private Date date;// 如果date传参扣错误提示：date需要是一个将来的时间

    /**
     * 嵌套校验
     */
    @NotNull
    @Valid
    private Job job;

    @Data
    public static class Job {

        @Min(value = 1, groups = GroupA.class)
        private Long jobId;

        @NotNull
        @Length(min = 2, max = 10)
        private String jobName;

        @NotNull
        @Length(min = 2, max = 10)
        private String position;
    }

    // @NotEmpty可以用来判断list不能为空
    @NotEmpty(groups = GroupA.class)
    // @Valid可实现list中对象的验证
    @Valid
    private List<UserDTO> userDTOList;

    // 有说法是list包的对象验证需要重新定义一个list，目前不需要再这样麻烦
    // @Valid
    // private ValidationList<UserDTO> userList;


}
